//---------------------------------------------------------------------------


#pragma hdrstop

#include "SaveForm.h"
#include <string.h>
#include <IniFiles.hpp>
#include <Grids.hpp>
#include <DBGrids.hpp>
//---------------------------------------------------------------------------

#pragma package(smart_init)
TSaveForm::TSaveForm(TForm *form, char *sFormName, char *sFileName)
{
        Form = form;
        if(strlen(sFormName) > 100) throw "To long name";
        if(strlen(sFileName) > 1000) throw "To long file name";
        strcpy(FormName, sFormName);
        strcpy(FileName, sFileName);
}
void TSaveForm::Save()
{
        TIniFile *ini = new TIniFile( ExtractFileDir(Application->ExeName) + "/"+ String(FileName));
        for(int i = 0; i<Form->ComponentCount; i++)
        {
             if(String(Form->Components[i]->ClassName()) == "TStringGrid")
             {
                TStringGrid *sgrid = (TStringGrid *) Form->Components[i];
                for(int j = 0; j<sgrid->ColCount; j++)
                {
                        ini->WriteInteger(String(FormName)+"."+Form->Components[i]->Name, String(j), sgrid->ColWidths[j]);
                }
             }
             if(String(Form->Components[i]->ClassName()) == "TDBGrid")
             {
                TDBGrid *dbgrid = (TDBGrid *) Form->Components[i];
                for(int j = 0; j<dbgrid->Columns->Count; j++)
                {
                        ini->WriteInteger(String(FormName)+"."+Form->Components[i]->Name, String(j), dbgrid->Columns->Items[j]->Width);
                }

             }

        }
        delete ini;
}
void TSaveForm::Load()
{
        TIniFile *ini = new TIniFile( ExtractFileDir(Application->ExeName) + "/"+ String(FileName));
        for(int i = 0; i<Form->ComponentCount; i++)
        {
             if(String(Form->Components[i]->ClassName()) == "TStringGrid")
             {
                TStringGrid *sgrid = (TStringGrid *) Form->Components[i];
                for(int j = 0; j<sgrid->ColCount; j++)
                {
                        sgrid->ColWidths[j] = ini->ReadInteger(String(FormName)+"."+Form->Components[i]->Name, String(j), sgrid->ColWidths[j]);
                }
             }
             if(String(Form->Components[i]->ClassName()) == "TDBGrid")
             {
                TDBGrid *dbgrid = (TDBGrid *) Form->Components[i];
                for(int j = 0; j<dbgrid->Columns->Count; j++)
                {
                        dbgrid->Columns->Items[j]->Width = ini->ReadInteger(String(FormName)+"."+Form->Components[i]->Name, String(j), dbgrid->Columns->Items[j]->Width);
                }

             }

        }
        delete ini;

}
